package risk.control.application.activity.major;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import risk.control.application.MainApplication;
import risk.control.application.R;
import risk.control.application.activity.base.BaseActivity;
import risk.control.application.entity.TransactionRecordBean;
import risk.control.application.utils.RouteJump;
import risk.control.application.utils.mmkv.MMKVUtils;
import risk.control.application.utils.mmkv.MMKV_KEY;

public class MainActivity extends BaseActivity {

    private TextView mTvTitle;
    private LinearLayout mBtnMore;

    private ListView mLvWithdrawal;
    private List<WithdrawalBean> mWithdrawalList;
    private MethodAdapter mMethodAdapter;

    private Button mBtnNext;

    private TextView mTvDistrict;
    private TextView mTvBank;
    private TextView mTvBranch;
    private TextView mTvAddress;
    private TextView mTvName;

    @Override
    protected void postData(HashMap<String, String> dataMap) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
        intiData();
        initListener();
    }

    private void intiView() {
        mTvTitle = findViewById(R.id.tv_info_title);
        mBtnMore = findViewById(R.id.btn_more);
        mLvWithdrawal = findViewById(R.id.lv_data);

        mBtnNext = findViewById(R.id.btn_next);

        mTvDistrict = findViewById(R.id.tv_district);
        mTvBank = findViewById(R.id.tv_bank);
        mTvBranch = findViewById(R.id.tv_branch);
        mTvAddress = findViewById(R.id.tv_address);
        mTvName = findViewById(R.id.tv_name);
    }

    @SuppressLint("SetTextI18n")
    private void intiData() {
        mTvTitle.setText("信息录入");
        //写入数据
        WithdrawalBean bean = new WithdrawalBean();
        bean.isSelect = "0";
        bean.des = "现场取款";
        WithdrawalBean bean1 = new WithdrawalBean();
        bean1.isSelect = "1";
        bean1.des = "预约取款";
        if (mWithdrawalList == null) {
            mWithdrawalList = new ArrayList<>();
        } else {
            mWithdrawalList.clear();
        }
        mWithdrawalList.add(bean);
        mWithdrawalList.add(bean1);
        //设置适配器
        mMethodAdapter = new MethodAdapter(this, mWithdrawalList);
        mLvWithdrawal.setAdapter(mMethodAdapter);
        //网点信息
        String userInfoData = MMKVUtils.getString(MMKV_KEY.KV_USER_INFO_DATA);
        if (userInfoData != null && !userInfoData.isEmpty()) {
            //解析数据
            JSONObject jsonObject = (JSONObject) JSON.parse(userInfoData);
            JSONObject data = jsonObject.getJSONObject("data");
            mTvDistrict.setText("区县：" + data.getString("districts"));
            mTvBank.setText("银行：" + data.getString("bankName"));
            mTvBranch.setText("网点：" + data.getString("branchName"));
            mTvAddress.setText("地址：" + data.getString("branchAddress"));
            mTvName.setText("网点人员：" + data.getString("userName"));
        }

    }

    private void initListener() {
        mBtnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转更多页面
                RouteJump.start(MainActivity.this, RouteJump.MORE_ACTION, null, false, true);
            }
        });

        mLvWithdrawal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int size = mWithdrawalList.size();
                for (int i = 0; i < size; i++) {
                    WithdrawalBean bean = mWithdrawalList.get(i);
                    if (i == position) {
                        bean.isSelect = "0";
                    } else {
                        bean.isSelect = "1";
                    }
                }
                mMethodAdapter.refreshData(mWithdrawalList);//刷新页面
            }
        });

        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转预约页面
                int size = mWithdrawalList.size();
                for (int i = 0; i < size; i++) {
                    WithdrawalBean bean = mWithdrawalList.get(i);
                    String isSelect = bean.isSelect;
                    String des = bean.des;
                    if (isSelect.equals("0")) {
                        //写入全局数据
                        TransactionRecordBean transactionRecordBean = MainApplication.transactionRecordBean;
                        transactionRecordBean.withdrawalType = des;
                        //跳转
                        Map<String, Object> params = new HashMap<>();
                        params.put("title", des);
                        RouteJump.start(MainActivity.this, RouteJump.MAKE_ACTION, params, false, true);
                    }
                }
            }
        });
    }

    private long firstTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                this.finish();
            } else {
                Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
